package jun.empdepmanagersystem02.service.impl;

import jun.empdepmanagersystem02.aop.LogFuntion;
import jun.empdepmanagersystem02.mapper.EmpMapper;
import jun.empdepmanagersystem02.pojo.Emp;
import jun.empdepmanagersystem02.pojo.PageMsg;
import jun.empdepmanagersystem02.pojo.Result;
import jun.empdepmanagersystem02.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Slf4j
@Service

public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;


    @Override
    @LogFuntion
    public Result delete(List<Integer> ids) {
        empMapper.delete(ids);
        return Result.success("删除成功");
    }

    @Override
    @LogFuntion
    public Result update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        return Result.success("更新成功");
    }

    @LogFuntion
    public Result selectList(Emp.Selector selector) {
        log.info("姓名:{}，性别:{},begin:{},end:{}", selector.getName(), selector.getGender(), selector.getBegin(), selector.getEnd());
        ;


        PageHelper.startPage(selector.getPage(), selector.getPageSize());
        List<Emp> empSelectList = empMapper.selectList(selector);
        Page<Emp> page = (Page<Emp>) empSelectList;

        if (page == null) {
            return Result.error("未查询到相关数据");
        } else if (page.getTotal() == 1 && selector.isMsgShow()) {
            return Result.success("个人信息查询成功", page.getResult().get(0));
        }
        return Result.success("数据获取成功", new PageMsg(page.getTotal(), page.getResult()));
    }

    @Override
    @LogFuntion
    public Result insert(Emp emp) {
        if (emp == null || emp.getName() == null || emp.getGender() == null || emp.getUsername() == null) {
            return Result.error("信息未填写完整 请重新输入");
        }

        emp.setEntrydate(new Date());
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        return Result.success("插入成功 插入数据如下", emp);
    }


    @Override

    public Emp getByUsername(String username) {
        return empMapper.getByUserName(username);
    }



}
